//
//package com.kly.chatter.api.application.task;
//
//import com.kly.chatter.api.application.service.AsyncSendMsgService;
//import com.kly.chatter.api.application.service.ChatterImRewardService;
//import com.kly.chatter.db.dao.ChatterImConfigDao;
//import com.kly.chatter.db.entity.ChatterImConfigEntity;
//import com.kly.chatter.db.entity.ChatterImRewardEntity;
//import com.kly.sc.api.application.utils.RpcUtils;
//import com.kly.service.MemberService;
//import com.kly.user.chatterService.ChatterUserService;
//import com.kly.user.dto.UserInfoDto;
//import com.kly.user.dto.UserMessageCountDto;
//import com.kly.user.service.UserMsgService;
//import com.kly.user.service.UserService;
//import lombok.extern.slf4j.Slf4j;
//import org.springframework.beans.factory.annotation.Autowired;
//import org.springframework.scheduling.annotation.Scheduled;
//import org.springframework.stereotype.Component;
//
//import javax.annotation.Resource;
//import java.math.BigDecimal;
//import java.text.SimpleDateFormat;
//import java.util.Date;
//import java.util.List;
//import java.util.Objects;
//import java.util.stream.Collectors;
//
///**
// * IM聊天奖励
// */
//@Slf4j
//@Component
//public class ChatterImRewardTask {
//
//    @Resource
//    private UserService userService;
//    @Resource
//    private ChatterUserService chatterUserService;
//
//    @Resource
//    private UserMsgService userMsgService;
//
//    @Autowired
//    private ChatterImConfigDao chatterImConfigDao;
//
//    @Autowired
//    private ChatterImRewardService chatterImRewardService;
//
//    @Resource
//    private MemberService memberService;
//
//
//    @Scheduled(fixedDelay = 30000)
//    public void execute() {
//        List<ChatterImConfigEntity> chatterImConfigEntities = chatterImConfigDao.selectAll();
//        if (!chatterImConfigEntities.isEmpty()) {
//            List<UserInfoDto> chatterList = RpcUtils.getResult(userService.getUserByUserType("1"));
//            for (UserInfoDto chatter : chatterList) {
//                List<String> modelUserCodes = RpcUtils.getResult(userMsgService.selectModelCodes(chatter.getUserCode()));
//                for (String modelUserCode : modelUserCodes) {
//                    List<UserMessageCountDto> userMessageCountDtoList = RpcUtils.getResult(userMsgService.selectTodayMessageCountInfo(chatter.getUserCode(), modelUserCode, chatterImConfigEntities.get(0).getStartCount()));
//                    for (UserMessageCountDto userMessageCountDto : userMessageCountDtoList) {
//                        SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
//                        int nowDate = Integer.parseInt(sdf.format(new Date()));
//                        ChatterImRewardEntity chatterImReward = chatterImRewardService.getLastRecord(chatter.getUserCode(), modelUserCode, userMessageCountDto.getUserCode(), null);
//                        List<ChatterImConfigEntity> configEntities;
//                        //判断是否是vip
//                        Boolean response = RpcUtils.getResult(memberService.ifMember(userMessageCountDto.getUserCode()));
//                        int ifMember = response ? 1 : 0;
//                        if (chatterImReward != null) {
//                            ChatterImConfigEntity chatterImConfig = chatterImConfigEntities.stream().filter(s -> null != s && Objects.equals(s.getId(), chatterImReward.getConfigId())).collect(Collectors.toList()).get(0);
//                            configEntities = chatterImConfigEntities.stream().filter(s -> null != s && s.getVip() == ifMember && s.getStartCount() > chatterImConfig.getStartCount()).collect(Collectors.toList());
//                        } else {
//                            configEntities = chatterImConfigEntities.stream().filter(s -> null != s && s.getVip() == ifMember).collect(Collectors.toList());
//                        }
//                        if (!configEntities.isEmpty()) {
//                            for (ChatterImConfigEntity chatterImConfigEntity : configEntities) {
//                                if (chatterImConfigEntity.getReward().compareTo(BigDecimal.ZERO) > 0 && userMessageCountDto.getMessageCount() > chatterImConfigEntity.getStartCount()) {
//                                    ChatterImRewardEntity chatterImRewardEntity = new ChatterImRewardEntity();
//                                    chatterImRewardEntity.setChatterCode(chatter.getUserCode());
//                                    chatterImRewardEntity.setModelCode(modelUserCode);
//                                    chatterImRewardEntity.setUserCode(userMessageCountDto.getUserCode());
//                                    chatterImRewardEntity.setMessageNum(userMessageCountDto.getMessageCount());
//                                    chatterImRewardEntity.setConfigId(chatterImConfigEntity.getId());
//                                    chatterImRewardEntity.setRewardDate(nowDate);
//                                    //判断是否已经奖励 没有的话  新增
//                                    Boolean haveChatterImReward = chatterImRewardService.haveChatterImReward(chatterImRewardEntity);
//                                    if (!haveChatterImReward) {
//                                        chatterImRewardEntity.setReward(chatterImConfigEntity.getReward());
//                                        chatterImRewardService.add(chatterImRewardEntity);
//                                    }
//                                }
//                            }
//                        }
//                    }
//                }
//            }
//        }
//    }
//
//}
